package com.sk.bilibili.view.activity;

import static com.google.android.material.appbar.AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED;
import static com.google.android.material.appbar.AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL;
import static com.google.android.material.appbar.AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL;
import static com.sk.bilibili.utils.StringUtils.str;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.widget.Toolbar;

import com.google.android.material.appbar.AppBarLayout;
import com.sk.bilibili.adapter.pager.videoSet.VideoSetFragmentAdapter;
import com.sk.bilibili.bean.Danmu;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.AppBarChangeCallBack;
import com.sk.bilibili.callBack.OnAppBarChangeListener;
import com.sk.bilibili.databinding.ActivityVideoSetBinding;
import com.sk.bilibili.event.VideoSetEvent;
import com.sk.bilibili.model.activity.VideoSetModel;
import com.sk.bilibili.view.diy.bottomDialog.VideoSetBottomDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class VideoSetActivity extends BaseActivity<ActivityVideoSetBinding, VideoSetModel> implements AppBarChangeCallBack {

    public static void start(Context context, Integer id) {
        start(context, id,0);
    }

    public static void start(Context context, Integer id,int selection) {
        Intent intent = new Intent(context, VideoSetActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("selection", selection);
        context.startActivity(intent);
    }

    private Integer videoSetId;
    private int selection;
    // 标题
    private Toolbar titleRoot;
    private VideoSetFragmentAdapter fragmentAdapter;
    private AppBarLayout.LayoutParams topParams;//使用参数 修改是否可以滚动
    private VideoSetBottomDialog bottomDialog;
    private InputMethodManager inputManager;

    @Override
    protected void afterSetView() {
        videoSetId = (Integer) getIntent().getSerializableExtra("id");
        selection=getIntent().getIntExtra("selection",0);
        inputManager = getSystemService(InputMethodManager.class);
        initView();
        bindEvent();
        loadData();
    }

    private void loadData() {
        model.loadVideoSet(videoSetId);
        model.loadDanmu(videoSetId);
    }

    private void bindEvent() {
        bind.ablShow.addOnOffsetChangedListener(new OnAppBarChangeListener(this));
        bind.title.tvPlay.setOnClickListener(v -> {
            bind.ablShow.setExpanded(true);
            bind.pvShow.start();
        });
        bind.title.ivBack.setOnClickListener(v->finish());
        bind.title.ivMore.setOnClickListener(v->bottomDialog.show(getSupportFragmentManager()));
        bind.tvSend.setOnClickListener(v -> {
            bind.llSend.setVisibility(View.VISIBLE);
            bind.etSend.requestFocus();
            inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        });
        bind.ivSend.setOnClickListener(v -> {
            Float time = bind.pvShow.getCurrentTime();
            Danmu danmu=model.sendDanmu(str(bind.etSend),videoSetId,time);
            bind.pvShow.addDanmu(danmu,true);
        });
        bind.ivDanmu.setOnClickListener(v -> bind.ivDanmu.setImageResource(bind.pvShow.changeDanmu()));
    }

    private void initView() {
        initVideo();
        initViewPager();
        initOther();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&bind.llSend.getVisibility()==View.VISIBLE){
            bind.llSend.setVisibility(View.GONE);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initOther() {
        bind.llSend.setVisibility(View.GONE);
        bottomDialog = new VideoSetBottomDialog(videoSetId);
    }

    private void initVideo() {
        titleRoot = bind.tbShow;
        titleRoot.setVisibility(View.GONE);
        topParams = (AppBarLayout.LayoutParams) bind.ctlShow.getLayoutParams();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleVideoSetEvent(VideoSetEvent videoSetEvent){
        switch (videoSetEvent.getType()){
            case 0:setPlay(videoSetEvent.getPlay());break;
            case 1:finish(); break;
            case 2:toast("TODO:全屏折叠Bug");break;
            case 3:bottomDialog.show(getSupportFragmentManager());break;
            case 4:bind.pvShow.play(videoSetEvent.getVideo());break;
        }
    }

    public void setPlay(boolean play){
        if(play){
            topParams.setScrollFlags(SCROLL_FLAG_NO_SCROLL);
        }else {
            topParams.setScrollFlags(SCROLL_FLAG_SCROLL|SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        }
        bind.ctlShow.setLayoutParams(topParams);
    }

    private void initViewPager() {
        fragmentAdapter = new VideoSetFragmentAdapter(getSupportFragmentManager(), videoSetId);
        bind.vpShow.setAdapter(fragmentAdapter);
        bind.tlShow.setupWithViewPager(bind.vpShow);
    }

    @Override
    public void onExpanding() {
        titleRoot.setVisibility(View.GONE);
    }

    @Override
    public void onExpand() {
        titleRoot.setVisibility(View.GONE);
    }

    @Override
    public void onClose() {
        titleRoot.setVisibility(View.VISIBLE);
    }

    public void loadVideoSetSuccess(VideoSet videoSet) {
        bind.pvShow.play(videoSet.getExtra().getVideos().get(selection));
        model.loadDanmu(videoSetId);
        fragmentAdapter.setDiscussNum(videoSet.getDiscussNum());
        fragmentAdapter.setSelection(selection);
        model.addHistory(videoSet);
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        bind.pvShow.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        bind.pvShow.pause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.pvShow.onDestroy();
    }

    public void loadDanmuSuccess(List<Danmu> danmus) {
        bind.pvShow.setDanmus(danmus);
        toast("弹幕已经全部加载完毕");
    }

    public void sendDanmuSuccess() {
        toast("弹幕发送成功");
        bind.etSend.setText("");
    }

}
























